package com.leviassaf.androidbasics;

import javax.xml.datatype.Duration;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputFilter.LengthFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

//89
public class WebBrowser extends Activity implements OnClickListener, android.content.DialogInterface.OnClickListener {
	
	WebView webviewBrowser;
	EditText edtUrl;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webbrowser);
		
		webviewBrowser = (WebView)findViewById(R.id.webviewBrowser);
		webviewBrowser.getSettings().setJavaScriptEnabled(true);
		webviewBrowser.getSettings().setLoadWithOverviewMode(true);
		webviewBrowser.getSettings().setUseWideViewPort(true);
		CustomWebViewClient customWebViewClient = new CustomWebViewClient();
		webviewBrowser.setWebViewClient(customWebViewClient);
		try{
			webviewBrowser.loadUrl("http://www.wi-q.co.il");
			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);		
		}
		catch(Exception e){
			e.printStackTrace();
		}

		//90
		Button btnGo = (Button)findViewById(R.id.btnGo);
		Button btnBack = (Button)findViewById(R.id.btnBack);
		Button btnForward = (Button)findViewById(R.id.btnForward);
		Button btnRefresh = (Button)findViewById(R.id.btnRefresh);
		Button btnClearHistory = (Button)findViewById(R.id.btnClearHistory);
		edtUrl = (EditText)findViewById(R.id.edtUrl);
		btnGo.setOnClickListener(this);
		btnBack.setOnClickListener(this);
		btnForward.setOnClickListener(this);
		btnRefresh.setOnClickListener(this);
		btnClearHistory.setOnClickListener(this);
		this.setTitle(String.format("%s-%s", this.getTitle().toString(), this.getClass().getSimpleName()));
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()){
		case R.id.btnGo:
			webviewBrowser.loadUrl(edtUrl.getText().toString());
			//93 displaying the keyboard for the edittext
			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
			break;
		case R.id.btnBack:
			if (webviewBrowser.canGoBack()){
				webviewBrowser.goBack();
			}
			break;
		case R.id.btnForward:
			if (webviewBrowser.canGoForward()){
				webviewBrowser.goForward();
			}
			break;
		case R.id.btnRefresh:
			webviewBrowser.reload();
			break;
		case R.id.btnClearHistory:
			confirmClearHistory();
			break;
		}
	}
	
	public void confirmClearHistory(){
		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
		alertDialogBuilder.setTitle("Confirm Delete...");
		alertDialogBuilder.setMessage("Clearing history will cause 'Back' and 'Forward' buttons to lose previous pages' history\nAre you sure you want to clear the history?");
		alertDialogBuilder.setIcon(R.drawable.critical);
		alertDialogBuilder.setPositiveButton("YES", this);
		alertDialogBuilder.setNegativeButton("NO", this);
		alertDialogBuilder.show();
	}

	//Class created to ensure that the web client for the browser
	//loads in the WebBrowser and not in a separate browser
	public class CustomWebViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			// TODO Auto-generated method stub
			return super.shouldOverrideUrlLoading(view, url);
		}

	}

	@Override
	public void onClick(DialogInterface dialog, int whichButton) {
		// TODO Auto-generated method stub
		switch (whichButton){
		case DialogInterface.BUTTON_POSITIVE:
			webviewBrowser.clearHistory();
			Toast.makeText(getApplicationContext(), "History cleared!!!", Toast.LENGTH_SHORT);
//			Toast.makeText(this, "History cleared!!!", Toast.LENGTH_SHORT).show();
			break;
		case DialogInterface.BUTTON_NEGATIVE:
			dialog.cancel();
			break;
		}
	}
}
